package net.yla.smartbus.module.ba.vehicle.detachment;

import net.yla.smartbus.core.enums.OperationTypeEnum;
import net.yla.smartbus.module.ba.employee.entity.EmployeeDO;
import net.yla.smartbus.module.ba.employee.mapper.EmployeeMapper;
import net.yla.smartbus.module.ba.employee.query.EmployeeQuery;
import net.yla.smartbus.module.ba.equipment.entity.BaDevicesDo;
import net.yla.smartbus.module.ba.equipment.mapper.DeviceMapper;
import net.yla.smartbus.module.ba.equipment.query.DeviceQuery;
import net.yla.smartbus.module.ba.group.entity.GroupEntity;
import net.yla.smartbus.module.ba.group.service.GroupService;
import net.yla.smartbus.module.ba.vehicle.dto.VehicleDto;
import net.yla.smartbus.module.ba.vehicle.entity.BaVehicleDriverRlDo;
import net.yla.smartbus.module.ba.vehicle.entity.BaVehiclesDo;
import net.yla.smartbus.module.ba.vehicle.mapper.VehicleStaffRelevanceMapper;
import net.yla.smartbus.module.ba.vehicle.query.VehicleStaffRelevanceQuery;
import net.yla.smartbus.module.ba.vehicle.utils.FourDate;
import net.yla.smartbus.module.ba.vehicletype.dto.VehicleTypeDto;
import net.yla.smartbus.module.ba.vehicletype.serice.VehicleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 默苍离
 * @date 2021/6/28 10:42
 * <p>
 * ******************************************
 * @annotation: 抽离的方法
 */
@Component
public class VehicleDetachment {

    @Autowired
    private GroupService groupService;

    @Autowired
    private VehicleTypeService vehicleTypeService;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private VehicleStaffRelevanceMapper vehicleStaffRelevanceMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 车辆 do转dto
     *
     * @param vehiclesDo
     * @return
     */
    public VehicleDto vehicleDto(BaVehiclesDo vehiclesDo) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        VehicleDto vehicleDto = new VehicleDto();
        BaVehiclesDo vehicle = vehiclesDo;


        if (vehicle.getVehicleId() != null) {
            vehicleDto.setVehicleId(vehicle.getVehicleId());
            vehicleDto.setNo(vehicle.getNo());
        }

        vehicleDto.setCode(vehicle.getCode());

        //设备信息
        DeviceQuery deviceQuery = new DeviceQuery();
        deviceQuery.setVehicleId(vehicle.getVehicleId());

        List<BaDevicesDo> deviceMapperList = deviceMapper.getList(deviceQuery);
        ArrayList<Integer> id = new ArrayList<>();
        ArrayList<String> name = new ArrayList<>();
        for (int i = 0; i < deviceMapperList.size(); i++) {
            BaDevicesDo devicesDo = deviceMapperList.get(i);
            id.add(devicesDo.getDeviceId());
            name.add(devicesDo.getNo());
        }
        vehicleDto.setDeviceId(id);
        vehicleDto.setDeviceName(name);

        //人员信息
        VehicleStaffRelevanceQuery relevanceQuery = new VehicleStaffRelevanceQuery();
        relevanceQuery.setVehicleId(vehicle.getVehicleId());
        List<BaVehicleDriverRlDo> data = vehicleStaffRelevanceMapper.getData(relevanceQuery);
        ArrayList<Integer> staffIdList = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            BaVehicleDriverRlDo rlDo = data.get(i);
            staffIdList.add(rlDo.getEmpId());
        }
        ArrayList<String> staffName = new ArrayList<>();
        for (Integer staffId : staffIdList) {
            EmployeeQuery employeeQuery = new EmployeeQuery();
            employeeQuery.setEmpId(staffId);
            List<EmployeeDO> emp = employeeMapper.findEmp(employeeQuery);
            if (emp.size() > 0) {
                staffName.add(emp.get(0).getName());
            }
        }
        vehicleDto.setOwnerId(staffIdList);
        vehicleDto.setOwnerName(staffName);

        //注册日期
        if (vehicle.getDate1() != null) {
            vehicleDto.setRegistrationDate(format.format(vehicle.getDate1()));
            vehicleDto.setDate1((vehicle.getDate1()));
        } else {
            vehicleDto.setRegistrationDate("---");
        }

        //购置日期
        if (vehicle.getDate2() != null) {
            vehicleDto.setBuyDate(format.format(vehicle.getDate2()));
            vehicleDto.setDate2((vehicle.getDate2()));
        } else {
            vehicleDto.setBuyDate("---");
        }

        //机构
        vehicleDto.setGroupId(vehicle.getGroupId());
        GroupEntity view = groupService.view(vehicle.getGroupId());
        if (view != null) {
            vehicleDto.setGroupName(view.getName());
        } else {
            vehicleDto.setGroupName("---");
        }

        //运营类型
        vehicleDto.setOperation(vehicle.getOperation());
        vehicleDto.setOperationName(OperationTypeEnum.getName(vehicle.getOperation()));

        //车辆类型
        vehicleDto.setModelId(vehicle.getModelId());
        if (vehicle.getModelId() != null && vehicle.getModelId() != 0) {
            VehicleTypeDto vehicleTypeDto = vehicleTypeService.getVehicleTypeById(vehicle.getModelId());
            if (vehicleTypeDto != null) {
                vehicleDto.setModelName(vehicleTypeDto.getBrandName() + " : " + vehicleTypeDto.getName());
                //品牌
                vehicleDto.setBrandId(vehicleTypeDto.getBrandId());
                //品牌回显
                List<Integer> list = new ArrayList<>();
                list.add(vehicleTypeDto.getBrandId());
                list.add(vehicle.getModelId());
                vehicleDto.setDeviceType(list);
            } else {
                vehicleDto.setModelName("---");
            }
        } else {
            vehicleDto.setModelName("---");
        }

        //行驶证
        if (vehicle.getDrivingNo() != null) {
            vehicleDto.setDrivingNo(vehicle.getDrivingNo());
        } else {
            vehicleDto.setDrivingNo("---");
        }
        //运营证
        if (vehicle.getOperationNo() != null) {
            vehicleDto.setOperationNo(vehicle.getOperationNo());
        } else {
            vehicleDto.setOperationNo("---");
        }

        vehicleDto.setPeriod(vehicle.getPeriod());
        vehicleDto.setInspect(FourDate.fourToDate(vehicle.getInspect()));
        vehicleDto.setCompulsory(FourDate.fourToDate(vehicle.getCompulsory()));
        vehicleDto.setPersonLiability(FourDate.fourToDate(vehicle.getPersonLiability()));
        vehicleDto.setVehicleDamage(FourDate.fourToDate(vehicle.getVehicleDamage()));
        vehicleDto.setInsureClaims1(vehicle.getInsureClaims1());
        vehicleDto.setInsureClaims2(vehicle.getInsureClaims2());
        vehicleDto.setInsureClaims3(vehicle.getInsureClaims3());

        if (vehicle.getMedigaps() != 0) {
            vehicleDto.setMedigaps(vehicle.getMedigaps());
        }

        if (vehicle.getOwnerType() != 0) {
            vehicleDto.setOwnerType(vehicle.getOwnerType());
        }

        //车辆照片
        List<String> images = new ArrayList<>();
        if (vehicle.getImage1() != null) {
            images.add(vehicle.getImage1());
        }
        if (vehicle.getImage2() != null) {
            images.add(vehicle.getImage2());
        }
        if (vehicle.getImage3() != null) {
            images.add(vehicle.getImage3());
        }
        if (vehicle.getImage4() != null) {
            images.add(vehicle.getImage4());
        }
        if (vehicle.getImage5() != null) {
            images.add(vehicle.getImage5());
        }
        vehicleDto.setImages(images);

        return vehicleDto;

    }


}
